const app = getApp()
Page({
  data:{},
  onShow:function(options){

  },
  recharge:function(e){
    console.log(e)
    var openid = app.globalData.userInfo.open_id;
    var money = e.detail.value.money;
    var patt1 = new RegExp('^[0-9]+$');
    var patt2 = patt1.test(money);
    if(!patt2){
      wx.showModal({
        title: '提示',
        content: '请输入正确的充值金额',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }else{
    if(openid && money){

      wx.request({
        url: app.globalData.apiUrl+'/api/pay/recharge',//改成你自己的链接
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        data:{money:money,openid:openid},
        method: 'POST',
        success: function (res) {
          console.log(res.data);
          console.log('调起支付');
          wx.requestPayment({
            'timeStamp': res.data.timeStamp,
            'nonceStr': res.data.nonceStr,
            'package': res.data.package,
            'signType': 'MD5',
            'paySign': res.data.paySign,
            'success': function (res) {
              console.log('success');
              wx.showToast({
                title: '支付成功',
                icon: 'success',
                duration: 3000
              });
            },
            'fail': function (res) {
              console.log('fail');
            },
            'complete': function (res) {
              console.log('complete');
            }
          });
        },
        fail: function (res) {
          console.log(res.data)
        }
      });

    }
    }
  }
})